from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QMainWindow, QWidget
from view import Ui_Window
from secondview import Ui_Form
from function import runLCS, runKnapsack
import random as rd
import string


class MyWindow(QMainWindow, Ui_Window):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle("最长公共子序列")
        self.input_x.setPlaceholderText("请输入任意字符串")
        self.input_y.setPlaceholderText("请输入任意字符串")
        self.run_Btn.clicked.connect(self.fun_run_Btn)
        self.test_Btn.clicked.connect(self.fun_test_Btn)
        self.show()

    def fun_run_Btn(self):
        x, y = self.input_x.text(), self.input_y.text()
        maxLen, maxSub = runLCS(x, y)
        self.output_len.setText(str(maxLen))
        self.output_substr.setText(maxSub)

    def fun_test_Btn(self):
        self.output_substr.clear()
        ans = "测试数据:"
        for i in range(5):
            x = ''.join(rd.choice(string.ascii_letters + string.digits) for _ in range(rd.randint(3, 300)))
            y = ''.join(rd.choice(string.ascii_letters + string.digits) for _ in range(rd.randint(1, 300)))
            maxlen, maxsubstr = runLCS(x, y)
            ans += "\n序列X：" + x + "\n序列Y：" + y + "\n最长公共子序列长度：" + str(maxlen) + "\n最长公共子序列：" + maxsubstr
        # print(ans)
        self.output_substr.setText(ans)


class SecWindow(QWidget, Ui_Form):
    def __init__(self):
        super(SecWindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle("0-1背包问题")
        self.input_n.setPlaceholderText("请输入一个正整数")
        self.input_c.setPlaceholderText("请输入一个实数")
        self.input_w.setPlaceholderText("请输入一个以英文逗号隔开的实数数组，数组长度请与之前输入的正整数一致")
        self.input_v.setPlaceholderText("请输入一个以英文逗号隔开的实数数组，数组长度请与之前输入的正整数一致")

        # 输入限制
        regx = QtCore.QRegExp("([0-9])*")
        intVal = QtGui.QRegExpValidator(self.input_n)
        intVal.setRegExp(regx)
        self.input_n.setValidator(intVal)
        regx = QtCore.QRegExp("([0-9](\.[0-9]+$)?)*")
        FlotVal = QtGui.QRegExpValidator(self.input_c)
        FlotVal.setRegExp(regx)
        self.input_c.setValidator(FlotVal)

        regx1 = QtCore.QRegExp("(([0-9]+(\.[0-9]+)?)\,)*")
        validator1 = QtGui.QRegExpValidator(self.input_w)
        validator1.setRegExp(regx1)
        self.input_w.setValidator(validator1)
        validator1 = QtGui.QRegExpValidator(self.input_v)
        validator1.setRegExp(regx1)
        self.input_v.setValidator(validator1)

        self.run_Btn.clicked.connect(self.fun_run_Btn)
        self.test_Btn.clicked.connect(self.fun_test_Btn)

    def fun_run_Btn(self):
        print("run")
        n = self.input_n.text()
        c = self.input_c.text()
        tmp_w = self.input_w.text()
        tmp_w = tmp_w.split(',')
        w = [float(x) for x in tmp_w if x is not '']
        tmp_v = self.input_v.text()
        tmp_v = tmp_v.split(',')
        v = [float(x) for x in tmp_v if x is not '']
        if '' in [n, c]:
            self.output.clear()
            self.output.setText("请先输入物品数目和背包承重")
            return
        else:
            n = int(n)
            c = float(c)
        if w.__len__() != n or v.__len__() != n:
            self.output.clear()
            self.output.setText("请正确输入“每样物品的重量和价值！”\n不要多输入或少输入")
            return
        else:
            maxvalue, x = runKnapsack(n, c, w, v)
            ans = "\n最大价值：" + str(maxvalue) + "\n背包中所选物品序号：" + ','.join([str(i) for i in range(len(x)) if x[i] == 1])
            self.output.setText(ans)

    def fun_test_Btn(self):
        self.output.clear()
        self.input_n.clear()
        self.input_c.clear()
        self.input_w.clear()
        self.input_v.clear()
        ans = "测试数据:"
        for _ in range(5):
            n = rd.randint(4, 20)  # 随机一个物品个数
            c = rd.randint(10, 200)  # 随机一个背包容量
            w = [rd.randint(1, c) for i in range(n)]
            v = [rd.randint(1, 1000) for i in range(n)]
            ans += "\n物品个数：" + str(n) + "\n背包容量：" + str(c) + "\n相应重量：" + str(w) + "\n价值：" + str(v)
            maxvalue, x = runKnapsack(n, c, w, v)
            ans += "\n最大价值：" + str(maxvalue) + "\n背包中所选物品序号：" + ','.join([str(i) for i in range(len(x)) if x[i] == 1])
        self.output.setText(ans)
